//
//  HeadControlView.m
//  RedManStock
//
//  Created by lanou3g on 15/7/18.
//  Copyright (c) 2015年 lanou3g. All rights reserved.
//

#import "HeadControlView.h"
#import "UIDefine.h"
#import "ParameterHeader.h"
@interface HeadControlView (){
    CGFloat width;
}

@end

@implementation HeadControlView


- (instancetype)initWithFrame:(CGRect)frame
                titleArray:(NSArray *)buttonTitleArray{
    self=[super initWithFrame:frame];
    if (self) {
        
        self.num=buttonTitleArray.count;
        _nowLocation=0;
        self.buttonTitleArray=[NSMutableArray arrayWithArray:buttonTitleArray];
        self.buttonArray=[NSMutableArray array];
        
        self.backgroundColor=TopBarColor;
        
        width=frame.size.width/_num;
        CGFloat barHeight=3;
        CGFloat buttonHeight=frame.size.height-barHeight;
        
        //button
        for (int i=0; i<self.num; i++) {
            UIButton *button=[UIButton buttonWithFrame:CGRectMake(i*width, 0, width, buttonHeight) normalTitle:buttonTitleArray[i]];
            
            //设置点击是是否有格式的变化，并设置两种状态的title颜色; 保证normal和highlighted一致，改变selected
            
            button.adjustsImageWhenHighlighted=NO;
            [button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
//            [button setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
//            [button setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
            button.titleLabel.font=[UIFont systemFontOfSize:15];
            
            button.tag=1000+i;
            
            if (i==0) {
                [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            }
            
#pragma mark test
//[button addTarget:self action:@selector(testClick:) forControlEvents:UIControlEventTouchUpInside];
            
            [self addSubview:button];
            [self.buttonArray addObject:button];
            
        }
        
        //redBarView
        self.redBarView=[[UIView alloc]initWithFrame:CGRectMake(0, buttonHeight, width, barHeight)];
        self.redBarView.backgroundColor=MyRedColor;
        
        [self addSubview:self.redBarView];
    }
    
    return self;
}

- (void)setNowLocation:(NSInteger)nowLocation{
    
//redBar的滚动动画
    
    [UIView animateWithDuration:0.2f animations:^{
        
//        self.redBarView.center=CGPointMake(self.redBarView.center.x+width, self.redBarView.center.y);
        
        UIButton *oldButton=self.buttonArray[_nowLocation];
        UIButton *newButton=self.buttonArray[nowLocation];
        [UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
        
        self.redBarView.center=CGPointMake(newButton.center.x, self.redBarView.center.y);
        
//        设置新旧button的Normal状态下颜色的变换
        [oldButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        [newButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        
        
        //可以通过属性字符串的方式来给button上的文字改变格式!!!!
//        [newButton setAttributedTitle:<#(NSAttributedString *)#> forState:<#(UIControlState)#>]
        
        //button的titleLabel和imageView都是系统懒加载来的，都是只读的，不能设置，如果button样式为system，则这两个属性都为nil
        //设置button上的字体，只要button不是system类型的，就可以用button.titleLabel.font来设置
    }];
    
    _nowLocation=nowLocation;
    
}

@end
